/*
 * Copyright (c) 2021. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package cn.edu.ntu.yyl.service.imlp;


import cn.edu.ntu.yyl.dao.UserFavoriteDao;
import cn.edu.ntu.yyl.pojo.article.Article;
import cn.edu.ntu.yyl.pojo.user.UserFavorite;
import cn.edu.ntu.yyl.pojo.user.UserInfo;
import cn.edu.ntu.yyl.service.UserFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserFavoriteServiceImpl implements UserFavoriteService {
    @Autowired
    private UserFavoriteDao Dao;
    //收藏
    public Integer InsertNewUserCollect(UserFavorite userFavorite) {
        //初始化
        
        try {
            //getMapper
            
            //执行SQL
            userFavorite.setId(getmaxid());
            Dao.InsertNewUserCollect(userFavorite);

        } catch (Exception e) {
            return -1;
        } finally {
            //提交事务
            
            //关闭连接
            
        }
        return 1;
    }
    public Integer DeleteUserCollect(UserFavorite userFavorite) {
        //初始化
        
        try {
            //getMapper
            
            //执行SQL
            Dao.DeleteUserCollect(userFavorite);

        } catch (Exception e) {
            return -1;
        } finally {
            //提交事务
            
            //关闭连接
            
        }
        return 1;
    }
    public Integer UpdateUserCollect(UserFavorite userFavorite) {
        //初始化
        
        try {
            //getMapper
            
            //执行SQL
            Dao.UpdateUserCollect(userFavorite);

        } catch (Exception e) {
            return -1;
        } finally {
            //提交事务
            
            //关闭连接
            
        }
        return 1;
    }
    public List<UserFavorite> SelectAllUserCollect() {
        //初始化
        
        try {
            //getMapper
            
            //执行SQL
            return Dao.SelectAllUserCollect();
        } catch (Exception e) {
            return null;
        } finally {
            //提交事务
            
            //关闭连接
            
        }
    }
    //点赞
    public Integer InsertNewUserFavorite(UserFavorite userFavorite){
        //初始化
        
        try {
            //getMapper
            
            //执行SQL
            userFavorite.setId(getmaxid());
            Dao.InsertNewUserFavorite(userFavorite);

        } catch (Exception e) {
            return -1;
        } finally {
            //提交事务
            
            //关闭连接
            
        }
        return 1;
    }
    public Integer DeleteUserFavorite(UserFavorite userFavorite){
        //初始化
        
        try {
            //getMapper
            
            //执行SQL
            Dao.DeleteUserFavorite(userFavorite);

        } catch (Exception e) {
            return -1;
        } finally {
            //提交事务
            
            //关闭连接
            
        }
        return 1;
    }
    public Integer UpdateUserFavorite(UserFavorite userFavorite){
        //初始化
        
        try {
            //getMapper
            
            //执行SQL
            Dao.UpdateUserFavorite(userFavorite);

        } catch (Exception e) {
            return -1;
        } finally {
            //提交事务
            
            //关闭连接
            
        }
        return 1;
    }
    public List<UserFavorite> SelectAllUserFavorite(){
        //初始化
        
        try {
            //getMapper
            
            //执行SQL
            return Dao.SelectAllUserFavorite();
        } catch (Exception e) {
            return null;
        } finally {
            //提交事务
            
            //关闭连接
            
        }
    }

    //按用户查询文章收藏
    public List<UserFavorite> SelectCollectByUserId(UserInfo userInfo){
        //初始化
        
        try {
            //getMapper
            
            //执行SQL
            return Dao.SelectCollectByUserId(userInfo);
        } catch (Exception e) {
            return null;
        } finally {
            //提交事务
            
            //关闭连接
            
        }
    }
    //查询文章收藏量
    public Integer SelectArticleCollectCount(Article article){
        //初始化
        
        try {
            //getMapper
            
            //执行SQL
            return Dao.SelectArticleCollectCount(article);
        } catch (Exception e) {
            return null;
        } finally {
            //提交事务
            
            //关闭连接
            
        }
    }
    //按用户查询文章点赞
    public List<UserFavorite> SelectFavoriteByUserId(UserInfo userInfo) {
        //初始化
        
        try {
            //getMapper
            
            //执行SQL
            return Dao.SelectFavoriteByUserId(userInfo);
        } catch (Exception e) {
            return null;
        } finally {
            //提交事务
            
            //关闭连接
            
        }
    }
    //查询文点赞量
    public Integer SelectArticleFavoriteCount(Article article) {
        //初始化
        
        try {
            //getMapper
            
            //执行SQL
            return Dao.SelectArticleCollectCount(article);
        } catch (Exception e) {
            return null;
        } finally {
            //提交事务
            
            //关闭连接
            
        }
    }

    private Integer getmaxid() {
        //初始化
        List<UserFavorite> s = null;
        Integer id = -1;
        //取得SqlSession对象(openSession)
        
        try {
            //getMapper
            
            //获取列表
            s = Dao.SelectAllUserFavorite();
        } catch (Exception e) {
            System.err.println(e.toString());

        } finally {
            //提交事务
            
            //关闭连接
            
        }
        //获取最大ID然后+1便是需要写入的ID
        if (s != null) {
            for (UserFavorite a : s) {
                if (id < a.getId()) {
                    id = a.getId();
                }
            }
        } else return -1;
        id++;
        return id;
    }
}
